<template>
  <a-modal title="部门选择" :visible="show" @ok="handleOk" @cancel="handleCancel" width="600px" :maskClosable="false">
    <div style="display:flex">
    <a-input v-model="ipagination.keyword" style="width:200px"/>
    <a-button type="primary" @click="fetchList">刷新</a-button>
    </div>
    <a-table
      :columns="columns"
      :data-source="records"
      rowKey="deptCode"
      bordered
      :pagination="ipagination"
      @change="handleTableChange"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
    </a-table>
  </a-modal>
</template>

<script>
import { getAction, postAction } from '@/api/manage'

export default {
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        total: 0,
      },
      columns: [
        {
          title: '部门编码',
          dataIndex: 'deptcode',
          key: 'deptcode',
        },
        {
          title: '部门名称',
          dataIndex: 'deptname',
          key: 'deptname',
        }
      ],
      records: [],
      urls: {
        list: '/depInfo/list',
      },
      currentSelection: [],
      selectedRowKeys: [],
    }
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    init() {
      this.currentSelection = []
      this.$set(this.ipagination, 'current', 1)
      this.$set(this.ipagination, 'pageSize', 10)
      this.selectedRowKeys = []
      this.fetchList()
    },
    async fetchList() {
      let searchForm = {}
      searchForm.current = this.ipagination.current;
      searchForm.pageSize = this.ipagination.pageSize;
      searchForm.keyword = this.ipagination.keyword;

      let res = await getAction(this.urls.list, searchForm)
      if (res.success) {
        this.records = res.result.records
        this.$set(this.ipagination, 'total', res.result.total)
      }
    },
    /**
     * 表结构切换页码
     */
    handleTableChange(pagination, filters, sorter) {
      this.ipagination = pagination
      this.ipagination.pageNo = this.ipagination.current
      this.fetchList()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.currentSelection = selectedRows
    },
    handleOk() {
      this.$emit('selected', this.currentSelection)
    },
    handleCancel(e) {
      this.$emit('selected', [])
    },
  },
}
</script>

<style>
</style> 